﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Proj5715.Process
{
    public partial class ProcessList : System.Web.UI.Page
    {
        UserPermissionBll userPermissionBll = new UserPermissionBll();

        private string FlowInstanceID
        {
            get
            {
                if (ViewState["FlowInstanceID"] != null)
                {
                    return ViewState["FlowInstanceID"].ToString();
                }
                else
                {
                    string flowInstanceID = Guid.NewGuid().ToString();
                    ViewState["FlowInstanceID"] = flowInstanceID;
                    return flowInstanceID;
                }
            }
            set
            {
                ViewState["FlowInstanceID"] = value;
            }
        }

        private string DispatchMissileID
        {
            get
            {
                if (ViewState["DispatchMissileID"] != null)
                {
                    return ViewState["DispatchMissileID"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                ViewState["DispatchMissileID"] = value;
            }
        }

        private string DispatchCode
        {
            get 
            {
                if (ViewState["DispatchCode"] != null)
                {
                    return ViewState["DispatchCode"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            set 
            { 
                ViewState["DispatchCode"] = value; 
            }
        }

        private string MissileModel
        {
            get
            {
                if (ViewState["MissileModel"] != null)
                {
                    return ViewState["MissileModel"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                ViewState["MissileModel"] = value;
            }
        }

        private string MissileCode
        {
            get
            {
                if (ViewState["MissileCode"] != null)
                {
                    return ViewState["MissileCode"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                ViewState["MissileCode"] = value;
            }
        }

        private string RepairType
        {
            get
            {
                if (ViewState["RepairType"] != null)
                {
                    return ViewState["RepairType"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                ViewState["RepairType"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FlowInstanceID = Request.QueryString["FlowInstanceID"].Trim();
                this.DispatchMissileID = Request.QueryString["DispatchMissileID"].Trim();
                InitControl(FlowInstanceID, DispatchMissileID);
                BindGrid(DispatchMissileID);

                // 下一步工序按钮
                ProcessDal processDal = new ProcessDal();
                DispatchMissileOB dispatchMissileOB = processDal.GetDispatchMissileById(this.DispatchMissileID);
                if (dispatchMissileOB != null)
                {
                    if (dispatchMissileOB.IsCompleted.ToLower().Equals("true"))
                    {
                        btnAddNext.Visible = false;
                    }
                    else
                    {
                        btnAddNext.Visible = true;
                        string url = string.Format("ProcessSelect.aspx?MissileMode={0}&RepairType={1}&DispatchMissileID={2}"
                            , this.MissileModel, this.RepairType, this.DispatchMissileID);
                        btnAddNext.Attributes.Add("onclick", string.Format("showPage('{0}','800px','600px');", url));
                    }
                }               
            }
        }

        private void BindGrid(string flowInstanceID)
        {
            ProcessDal dal = new ProcessDal();
            var datasource = dal.GetDispatchMissileProcessByDispatchMissileID(flowInstanceID);

            grv.DataSource = datasource;
            grv.DataBind();
        }

        private void InitControl(string flowInstanceID, string DispatchMissileID)
        {
            try
            {
                ProcessDal dal = new ProcessDal();

                DispatchOB dispatchOB = dal.GetDispatchByID(flowInstanceID);
                if (dispatchOB != null)
                {
                    lbl_DispatchCode.Text = dispatchOB.DispatchCode;
                    lbl_planCompleteTime.Text =  Common.Helper.Instance.ConvertToShotDate(dispatchOB.PlanCompletionDate);
                    lbl_RepairType.Text = dispatchOB.RepairType;
                    this.DispatchCode = dispatchOB.DispatchCode;
                    this.MissileModel = dispatchOB.MissileModel;
                    this.RepairType = dispatchOB.RepairType;
                }

                DispatchMissileOB dispatchMissileOB = dal.GetDispatchMissileById(DispatchMissileID);
                if (dispatchMissileOB != null)
                {
                    lbl_MissileCode.Text = dispatchMissileOB.MissileCode;
                    lbl_RemainingLife.Text = dispatchMissileOB.RemainingLife;
                    this.MissileCode = dispatchMissileOB.MissileCode;
                }

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string dispatchMissileID = this.DispatchMissileID;

            ProcessDal dal = new ProcessDal();
            switch (e.CommandName)
            {
                case "btnDelete":                    
                    dal.DispatchMissileProcess_Delete(e.CommandArgument.ToString());
                    BindGrid(dispatchMissileID);
                    break;
                case "BeginRepair" :
                    BindGrid(dispatchMissileID);
                    break;
                case "Fault" :
                    BindGrid(dispatchMissileID);
                    break;
                case "Exchange" :
                    BindGrid(dispatchMissileID);
                    break;                
                case "Complete" :
                    dal.SetProcessComplete(e.CommandArgument.ToString());
                    BindGrid(dispatchMissileID);
                    break;
                case "Check" :
                    BindGrid(dispatchMissileID);
                    break;
                //case "Next" :
                //    BindGrid(dispatchMissileID);
                //    break;
                default:
                    break;
            }
        }

        protected void btn_back_Click(object sender, EventArgs e)
        {
            Response.Redirect("DispatchEdit.aspx?flowInstanceID=" + FlowInstanceID + "&Source=list");
        }

        protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            ProcessDal processDal = new ProcessDal();
            string dispatchMissileProcessID = grv.DataKeys[e.Row.RowIndex]["DispatchMissileProcessID"].ToString();
            string processSortNumber = grv.DataKeys[e.Row.RowIndex]["ProcessSortNumber"].ToString();
            string processDeptID = grv.DataKeys[e.Row.RowIndex]["DeptID"].ToString();

            // 获取工序对象
            DispatchMissileProcessOB dispatchMissileProcessOB = processDal.GetDispatchMissileProcessById(dispatchMissileProcessID);

            // 是否领修
            bool isBeginRepair = processDal.IsBeginRepair(dispatchMissileProcessOB);

            // 是否完成
            bool isCompleted = processDal.IsCompleted(dispatchMissileProcessOB);

            #region 格式化数据

            // 是否故障
            e.Row.Cells[5].Text = e.Row.Cells[5].Text.ToLower().Equals("true") ? "是" : "否";

            // 是否串件
            int ExchangePartCount = processDal.GetExchangePartObList(dispatchMissileProcessID).Count;
            e.Row.Cells[6].Text = ExchangePartCount > 0 ? "是" : "否";

            // 是否损耗
            int wastageCount = processDal.GetWastageInfo(this.DispatchCode, this.MissileCode, processSortNumber).Rows.Count;
            e.Row.Cells[7].Text = wastageCount > 0 ? "是" : "否";

            // 是否完工
            e.Row.Cells[8].Text = e.Row.Cells[8].Text.ToLower().Equals("true") ? "是" : "否";

            // 检验结果
            switch (e.Row.Cells[10].Text)
            {
                case "1":
                    e.Row.Cells[10].Text = "合格";
                    break;
                case "2" :
                    e.Row.Cells[10].Text = "不合格";
                    break;
                default:
                    e.Row.Cells[10].Text = string.Empty;
                    break;
            }
            
            // 不合格类型
            switch (e.Row.Cells[11].Text)
            { 
                case "1":
                    e.Row.Cells[11].Text = "返修";
                    break;
                case "2":
                    e.Row.Cells[11].Text = "返工";
                    break;
                case "3":
                    e.Row.Cells[11].Text = "报废";
                    break;
                case "4":
                    e.Row.Cells[11].Text = "让步使用";
                    break;
                default:
                    break;
            }

            #endregion

            Button btnDelete = e.Row.FindControl("btnDelete") as Button;
            if (btnDelete != null)
            {
                btnDelete.Visible = !isBeginRepair;

                // 当前用户的部门ID与工序负责部门的ID对应时，才可以显示按钮。
                if (btnDelete.Visible)
                {
                    btnDelete.Visible = processDeptID.Equals(CurrentInfo.Instance.CurrentUser.Dept.DeptID);
                }
            }

            Button btnBeginRepair = e.Row.FindControl("btnBeginRepair") as Button;
            if (btnBeginRepair != null)
            {
                string url = string.Format("ProcessEdit.aspx?DispatchMissileProcessID={0}", dispatchMissileProcessID);
                btnBeginRepair.Attributes.Add("onclick", string.Format("showPage('{0}','400px','200px');", url));

                btnBeginRepair.Visible = !isBeginRepair;

                // 当前用户的部门ID与工序负责部门的ID对应时，才可以显示按钮。
                if (btnBeginRepair.Visible)
                {
                    btnBeginRepair.Visible = processDeptID.Equals(CurrentInfo.Instance.CurrentUser.Dept.DeptID);
                }
            }

            Button btnFault = e.Row.FindControl("btnFault") as Button;
            if (btnFault != null)
            {
                if (!isBeginRepair || isCompleted)
                {
                    btnFault.Visible = false;
                }

                string url = string.Format("FaultEdit.aspx?DispatchMissileProcessID={0}", dispatchMissileProcessID);
                btnFault.Attributes.Add("onclick", string.Format("showPage('{0}','400px','300px');", url));

                // 当前用户的部门ID与工序负责部门的ID对应时，才可以显示按钮。
                if (btnFault.Visible)
                {
                    btnFault.Visible = processDeptID.Equals(CurrentInfo.Instance.CurrentUser.Dept.DeptID);
                }
            }            

            Button btnExchange = e.Row.FindControl("btnExchange") as Button;
            if (btnExchange != null)
            {
                if (!isBeginRepair || isCompleted)
                {
                    btnExchange.Visible = false;
                }

                string url = string.Format("ProcessExchangePart.aspx?DispatchMissileProcessID={0}", dispatchMissileProcessID);
                btnExchange.Attributes.Add("onclick", string.Format("showPage('{0}','1000px','600px');", url));

                // 当前用户的部门ID与工序负责部门的ID对应时，才可以显示按钮。
                if (btnExchange.Visible)
                {
                    btnExchange.Visible = processDeptID.Equals(CurrentInfo.Instance.CurrentUser.Dept.DeptID);
                }
            }

            Button btnWastage = e.Row.FindControl("btnWastage") as Button;
            if (btnWastage != null)
            {
                if (!isBeginRepair || isCompleted)
                {
                    btnWastage.Visible = false;
                }

                string url = string.Format("ProcessWastage.aspx?DispatchCode={0}&MissileCode={1}&ProcessSortNumber={2}", 
                    this.DispatchCode, this.MissileCode, processSortNumber);
                btnWastage.Attributes.Add("onclick", string.Format("showPage('{0}','800px','600px');", url));

                // 当前用户的部门ID与工序负责部门的ID对应时，才可以显示按钮。
                if (btnWastage.Visible)
                {
                    btnWastage.Visible = processDeptID.Equals(CurrentInfo.Instance.CurrentUser.Dept.DeptID);
                }
            }

            Button btnCheck = e.Row.FindControl("btnCheck") as Button;
            if (btnCheck != null)
            {
                if (isBeginRepair && isCompleted && string.IsNullOrWhiteSpace(dispatchMissileProcessOB.InspectionState)
                    && userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.ProcessCheck))
                {
                    btnCheck.Visible = true;
                }
                else
                {
                    btnCheck.Visible = false;
                }

                if (btnCheck.Visible)
                {
                    btnCheck.Visible = processDal.CanCheckProcess(CurrentInfo.Instance.CurrentUser.ID, processDeptID);
                }

                string url = string.Format("ProcessCheck.aspx?DispatchMissileProcessID={0}", dispatchMissileProcessID);
                btnCheck.Attributes.Add("onclick", string.Format("showPage('{0}','400px','300px');", url));
            }            

            Button btnComplete = e.Row.FindControl("btnComplete") as Button;
            if (btnComplete != null)
            {
                if (!isBeginRepair || isCompleted)
                {
                    btnComplete.Visible = false;
                }

                // 当前用户的部门ID与工序负责部门的ID对应时，才可以显示按钮。
                if (btnComplete.Visible)
                {
                    btnComplete.Visible = processDeptID.Equals(CurrentInfo.Instance.CurrentUser.Dept.DeptID);
                }
            }
        }

        protected void btnAddNext_Click(object sender, EventArgs e)
        {
            BindGrid(this.DispatchMissileID);
        }
    }
}